<template>
	<view class="examine-container">


		<!-- 标签选择器 -->
		<view class="tab-container">
			<view class="tab-wrapper">
				<view class="tab-item" :class="{ active: activeTab === 'toBeReviewed' }"
					@click="handleTable('toBeReviewed')">
					<text>To be reviewed</text>
					<view class="badge" v-if="splength">{{ splength || '' }}</view>
				</view>
				<view class="tab-item" :class="{ active: activeTab === 'reviewed' }"
					@click="handleTable('reviewed')">
					<text>Reviewed</text>
				</view>
			</view>
		</view>

		<!-- 搜索框 -->
		<view class="search-container">
			<view class="search-box">
				<image class="search-icon" src="/static/uniapp/search.png" mode="scaleToFill" />
				<input inputmode="search " @confirm="getList()" type="text" class="search-input" placeholder="input" v-model="searchText" />
			</view>
		</view>

		<!-- 申请列表 -->
		<view class="application-list">
			<!-- 待审核项目 -->
			<view class="application-item" v-for="(item, index) in applications" :key="index"
				@click="viewApplication(item)">
				<!-- 申请人信息 -->
				<view class="applicant-info">
					<view class="applicant-info-top">
						<image class="avatar" :src="item.user.avatar" mode="aspectFill"></image>
						<view class="application-basic">
							<view class="application-name">{{ item.user.username }} - {{ item.user.department || 'No positions available' }}</view>
						</view>
					</view>
					<text class="application-time">{{ $dayjs(item.created_at).format('YYYY-MM-DD hh:MM:ss') }}</text>
				</view>

				<!-- 申请详情 -->
				<view class="application-details">
					<view class="detail-row">
						<text class="detail-label">Initiate:</text>
						<text class="detail-value">{{ $dayjs(item.start_time).format('YYYY-MM-DD hh:MM:ss') }}</text>
					</view>
					<view class="detail-row">
						<text class="detail-label">Finish:</text>
						<text class="detail-value">{{ $dayjs(item.end_time).format('YYYY-MM-DD hh:MM:ss') }}</text>
					</view>
					<view class="detail-row">
						<text class="detail-label">Duration:</text>
						<text class="detail-value">{{ item.days }}</text>
					</view>
				</view>

				<!-- 状态标签 -->
				<view class="status-tag" :class="'status-' + item.status">
					{{ getStatusText(item.status) }}
				</view>


			</view>
		</view>
		<uv-load-more :status="loadingType" />
	</view>
</template>

<script>
import { getreplacementList } from "@/api/approval"
import { onShow } from "@dcloudio/uni-app";
export default {
	data() {
		return {
			activeTab: 'toBeReviewed',
			searchText: '',
			splength: '0',
			page: 1,
			loadingType:'',
			applications: [
			]
		}
	},
	computed: {
		filteredApplications() {
			if (this.searchText.trim() === '') {
				return this.applications;
			}

			const searchLower = this.searchText.toLowerCase();
			return this.applications.filter(item => {
				return item.name.toLowerCase().includes(searchLower) ||
					item.type.toLowerCase().includes(searchLower);
			});
		}
	},
	created() {
	},
	onPullDownRefresh() {
			console.log('onPullDownRefresh')
			this.getList();
			uni.stopPullDownRefresh();
		},
		onShow(){
			this.getList()
		},
		onReachBottom() {
			console.log('onReachBottom')
			this.getList("more");
		},
	methods: {
		handleTable(activeTab){
			this.activeTab = activeTab,
			this.getList()
		},
		async getList(type = 'refresh') {
			if (type === "refresh") {
				this.page = 1;
				this.applications = []
			} else if (this.loadingType === "nomore") {
				return;
			}
			this.loadingType = "loading";
			let apiData = {
				status: this.activeTab == 'toBeReviewed' ? 1 : -1,
				searchText: this.searchText,
				page: this.page,
				limit: 10,
			}
			console.log('getList', apiData)
			let { list, total } = await getreplacementList(apiData)
			if (list.length < 10) {
				if (type === "refresh") this.loadingType = "nodata";
				if (type === "more") this.loadingType = "nomore";
			}
			if(this.activeTab == 'toBeReviewed'){
				this.splength = total
			}
			this.page++
			this.applications = type === "refresh" ? list : this.applications.concat(list);
		},
		getStatusText(status) {
			switch (status) {
				case '1'://待审核
					return '待审核';
				case '0'://草稿
					return '草稿';
				case '2'://审核中
					return '审核中';
				case '3'://通过审核
					return '通过审核';
				case '4'://驳回
					return '驳回';
				case '5'://撤销
					return '撤销';
				default:
					return '';
			}
		},
		viewApplication(item) {
			uni.navigateTo({
				url: '/pages/Approval/approvalDetail?id='+item.id
			})
		}


	}
}
</script>

<style lang="scss" scoped>
page {
	background-color: #f5f5f5;
	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

.examine-container {
	display: flex;
	flex-direction: column;
	min-height: calc(100vh - 200px);
	position: relative;
	margin-top: 35rpx;
	padding-bottom: 130rpx;
	/* 为底部导航留出空间 */
}



/* 标签选择器 */
.tab-container {
	padding: 0 30rpx 20rpx;
}

.tab-wrapper {
	display: flex;
	border-radius: 50rpx;
	overflow: hidden;
	background-color: #f0f0f0;
	height: 62rpx;
}

.tab-item {
	flex: 1;
	display: flex;
	align-items: center;
	justify-content: center;
	font-weight: 500;
	font-size: 12px;
	color: #FEFEFE;
	line-height: 16px;
	color: #666;
	position: relative;
}

.tab-item.active {
	background-color: #127F4E;
	color: #fff;
	border-radius: 50rpx;
	font-weight: 500;
}

.badge {
	font-weight: 500;
	font-size: 20rpx;
	color: #FEFEFE;
	line-height: 26rpx;
	border-radius: 50%;
	background-color: #ff9800;
	color: #fff;
	font-size: 24rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-left: 10rpx;
	padding: 2px 5px;
}

/* 搜索框 */
.search-container {
	padding: 20rpx 30rpx;
}

.search-box {
	display: flex;
	align-items: center;
	height: 80rpx;
	border-radius: 40rpx;
	background-color: #fff;
	padding: 0 30rpx;
}

.search-icon {
	width: 40rpx;
	height: 40rpx;
	margin-right: 20rpx;
	opacity: 0.5;
}

.search-input {
	flex: 1;
	height: 80rpx;
	font-size: 30rpx;
	color: #333;
}

/* 申请列表 */
.application-list {
	padding: 0 30rpx;
}

.application-item {
	background-color: #fff;
	border-radius: 20rpx;
	margin-bottom: 30rpx;
	position: relative;
	box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05);
}

.applicant-info {
	position: relative;
	padding: 12px 14px 6px 14px;
	font-weight: normal;
	font-size: 13px;
	color: #0E623D;
	text-align: left;
	border-bottom: 1px solid #EAECF0;

	.applicant-info-top {
		display: flex;
		align-items: center;
	}
}

.avatar {
	width: 46rpx;
	height: 46rpx;
	border-radius: 50%;
	margin-right: 20rpx;
}

.application-basic {
	flex: 1;
	padding-right: 120rpx;
	/* 为时间留出空间 */
}

.application-name {
	font-size: 32rpx;
	font-weight: 500;
	color: #19B36E;
	margin-bottom: 0;
	/* 减少下边距 */
}

.application-time {
	color: #999;
	font-weight: 400;
	font-size: 11px;
	text-align: right;
}

.application-details {
	margin-bottom: 20rpx;
	padding: 6px 14px;
}

.detail-row {
	display: flex;
	margin: 12rpx 0;
}

.detail-label {
	width: 150rpx;
	font-weight: 500;
	font-size: 26rpx;
	color: #555555;
	text-align: left;
}

.detail-value {
	flex: 1;
	font-size: 24rpx;
	color: #333;
}

.status-tag {
	position: absolute;
	right: 30rpx;
	bottom: 30rpx;
	padding: 12rpx 16rpx;
	border-radius: 30rpx;
	font-size: 24rpx;
	background: #1751A3;
	color: #fff;
}

.status-unsent {
	background-color: #1976d2;
}

.status-withdrawn {
	background-color: #1976d2;
}

.status-returned {
	background-color: #999;
}
</style>